1 /* 2 * Copyright (C) 2007 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.io; 18 19 import com.google.common.annotations.Beta; 20 import com.google.common.base.Preconditions; 21 import com.google.common.primitives.Longs; 22 23 import java.io.DataOutput; 24 import java.io.DataOutputStream; 25 import java.io.FilterOutputStream; 26 import java.io.IOException; 27 import java.io.OutputStream; 28 29 /** 30 * An implementation of {@link DataOutput} that uses little-endian byte ordering 31 * for writing {@code char}, {@code short}, {@code int}, {@code float}, {@code 32 * double}, and {@code long} values. 33 * <p> 34 * <b>Note:</b> This class intentionally violates the specification of its 35 * supertype {@code DataOutput}, which explicitly requires big-endian byte 36 * order. 37 * 38 * @author Chris Nokleberg 39 * @author Keith Bottner 40 * @since 8.0 41 */ 42 @Beta 43 public class LittleEndianDataOutputStream extends FilterOutputStream 44 implements DataOutput { 45 46 /** 47 * Creates a {@code LittleEndianDataOutputStream} that wraps the given stream. 48 * 49 * @param out the stream to delegate to 50 */ 51 public LittleEndianDataOutputStream(OutputStream out) { 52 super(new DataOutputStream(Preconditions.checkNotNull(out))); 53 } 54 55 @Override public void write(byte[] b, int off, int len) throws IOException { 56 // Override slow FilterOutputStream impl 57 out.write(b, off, len); 58 } 59 60 @Override public void writeBoolean(boolean v) throws IOException { 61 ((DataOutputStream) out).writeBoolean(v); 62 } 63 64 @Override public void writeByte(int v) throws IOException { 65 ((DataOutputStream) out).writeByte(v); 66 } 67 68 /** 69 * @deprecated The semantics of {@code writeBytes(String s)} are considered 70 * dangerous. Please use {@link #writeUTF(String s)}, 71 * {@link #writeChars(String s)} or another write method instead. 72 */ 73 @Deprecated 74 @Override public void writeBytes(String s) throws IOException { 75 ((DataOutputStream) out).writeBytes(s); 76 } 77 78 /** 79 * Writes a char as specified by {@link DataOutputStream#writeChar(int)}, 80 * except using little-endian byte order. 81 * 82 * @throws IOException if an I/O error occurs 83 */ 84 @Override public void writeChar(int v) throws IOException { 85 writeShort(v); 86 } 87 88 /** 89 * Writes a {@code String} as specified by 90 * {@link DataOutputStream#writeChars(String)}, except each character is 91 * written using little-endian byte order. 92 * 93 * @throws IOException if an I/O error occurs 94 */ 95 @Override public void writeChars(String s) throws IOException { 96 for (int i = 0; i < s.length(); i++) { 97 writeChar(s.charAt(i)); 98 } 99 } 100 101 /** 102 * Writes a {@code double} as specified by 103 * {@link DataOutputStream#writeDouble(double)}, except using little-endian 104 * byte order. 105 * 106 * @throws IOException if an I/O error occurs 107 */ 108 @Override public void writeDouble(double v) throws IOException { 109 writeLong(Double.doubleToLongBits(v)); 110 } 111 112 /** 113 * Writes a {@code float} as specified by 114 * {@link DataOutputStream#writeFloat(float)}, except using little-endian byte 115 * order. 116 * 117 * @throws IOException if an I/O error occurs 118 */ 119 @Override public void writeFloat(float v) throws IOException { 120 writeInt(Float.floatToIntBits(v)); 121 } 122 123 /** 124 * Writes an {@code int} as specified by 125 * {@link DataOutputStream#writeInt(int)}, except using little-endian byte 126 * order. 127 * 128 * @throws IOException if an I/O error occurs 129 */ 130 @Override public void writeInt(int v) throws IOException { 131 out.write(0xFF & v); 132 out.write(0xFF & (v >> 8)); 133 out.write(0xFF & (v >> 16)); 134 out.write(0xFF & (v >> 24)); 135 } 136 137 /** 138 * Writes a {@code long} as specified by 139 * {@link DataOutputStream#writeLong(long)}, except using little-endian byte 140 * order. 141 * 142 * @throws IOException if an I/O error occurs 143 */ 144 @Override public void writeLong(long v) throws IOException { 145 byte[] bytes = Longs.toByteArray(Long.reverseBytes(v)); 146 write(bytes, 0, bytes.length); 147 } 148 149 /** 150 * Writes a {@code short} as specified by 151 * {@link DataOutputStream#writeShort(int)}, except using little-endian byte 152 * order. 153 * 154 * @throws IOException if an I/O error occurs 155 */ 156 @Override public void writeShort(int v) throws IOException { 157 out.write(0xFF & v); 158 out.write(0xFF & (v >> 8)); 159 } 160 161 @Override public void writeUTF(String str) throws IOException { 162 ((DataOutputStream) out).writeUTF(str); 163 } 164 165 // Overriding close() because FilterOutputStream's close() method pre-JDK8 has bad behavior: 166 // it silently ignores any exception thrown by flush(). Instead, just close the delegate stream. 167 // It should flush itself if necessary. 168 @Override public void close() throws IOException { 169 out.close(); 170 } 171 }